/*******************************************************************************
 * @(#)UserDao.java 2019年02月26日 17:12
 * Copyright 2019 upbos.com. All rights reserved.
 *******************************************************************************/
package com.upbos.upm.user;

import com.upbos.data.core.Pagination;
import com.upbos.data.core.SingleDao;
import com.upbos.upm.entity.Role;
import com.upbos.upm.entity.User;
import com.upbos.upm.entity.UserCfg;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <b>Application name：</b> UserDao.java <br>
 * <b>Application describing： </b> <br>
 * <b>Copyright：</b> Copyright &copy; 2019 upbos.com 版权所有。<br>
 * <b>Company：</b> upbos.com <br>
 * <b>Date：</b> 2019年02月26日 17:12 <br>
 * <b>author：</b> <a href="mailto:wangjz@miyzh.com"> wangjz </a> <br>
 * <b>version：</b>V5.0.0 <br>
 */
@Repository
public class UserDao {

    @Resource
    private SingleDao singleDao;

    public Pagination listUserByOrgId(Integer pageNo, Integer pageSize, String orgId) {
        return singleDao.queryPaginationWithTotal(pageNo, pageSize, "UserDaoMapper.listUserByOrgId", orgId);
    }

    public Pagination listUserByDeptId(Integer pageNo, Integer pageSize, String deptId) {
        return singleDao.queryPaginationWithTotal(pageNo, pageSize, "UserDaoMapper.listUserByDeptId", deptId);
    }

    public Pagination listUserByName(Integer pageNo, Integer pageSize, String orgId, String name) {
        User user = new User();
        user.setOrgId(orgId);
        user.setName(name);
        return singleDao.queryPaginationWithTotal(pageNo, pageSize, "UserDaoMapper.listUserByName", user);
    }

    public List<Role> listUserRole(String uid) {
        return singleDao.queryList("UserDaoMapper.listUserRole", uid);
    }

    public List<Role> listRole(String orgId) {
        return singleDao.queryList("UserDaoMapper.listRole", orgId);
    }

    public User getUserByUid(String uid) {
        return singleDao.queryOne("UserDaoMapper.getUser", uid);
    }

    public User insertUser(User user) {
        singleDao.insert(user);
        return user;
    }

    public void updateUser(User user) {
        singleDao.updateById(user, "password");
    }

    public void updatePassword(User user) {
        singleDao.updateById(user);
    }

    public void deleteUser(String uid) {
        User user = new User();
        user.setUid(uid);
        singleDao.deleteById(user);
    }

    public void insertUserRole(String uid, String roles) {
        if ("".equals(roles)) {
            return;
        }
        String[] roleArray = roles.split(",");
        List<Map<String, String>> roleList = new ArrayList<>();
        for (String role : roleArray) {
            Map<String, String> item = new HashMap<>(2);
            item.put("user_id", uid);
            item.put("role_id", role);
            roleList.add(item);
        }
        singleDao.update("UserDaoMapper.insertUserRole", roleList);
    }

    public void deleteUserRole(String uid) {
        singleDao.delete("UserDaoMapper.deleteUserRole", uid);
    }

    public boolean existLoginName(String loginName) {
        int cnt = singleDao.queryOne("UserDaoMapper.existLoginName", loginName);
        return cnt > 0 ? true : false;
    }

    public void resetPassword(User user) {
        singleDao.updateById(user);
    }

    public UserCfg getUserCfg(String uid, String key) {
        Map<String, Object> p = new HashMap<String, Object>(2);
        p.put("uid", uid);
        p.put("key", key);
        return singleDao.queryOne("UserDaoMapper.getUserCfg", p);
    }

    public List<UserCfg> listUserCfg(String uid, String[] keys) {
        Map<String, Object> p = new HashMap<String, Object>(2);
        p.put("uid", uid);
        p.put("keys", keys);
        List<UserCfg> userCfgs = singleDao.queryList("UserDaoMapper.listUserCfg", p);
        return userCfgs;
    }
}